package com.ruoyi.publish.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.publish.domain.IdReport;
import com.ruoyi.publish.service.IIdReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.publish.mapper.ApprovalRecordMapper;
import com.ruoyi.publish.domain.ApprovalRecord;
import com.ruoyi.publish.service.IApprovalRecordService;
import com.ruoyi.common.core.text.Convert;

/**
 * 审批记录信息Service业务层处理
 *
 * @author confusefox
 * @date 2020-12-15
 */
@Service
public class ApprovalRecordServiceImpl implements IApprovalRecordService {
    @Autowired
    private ApprovalRecordMapper approvalRecordMapper;

    @Autowired
    private IIdReportService reportService;

    /**
     * 查询审批记录信息
     *
     * @param id 审批记录信息ID
     * @return 审批记录信息
     */
    @Override
    public ApprovalRecord selectApprovalRecordById(Long id) {
        return approvalRecordMapper.selectApprovalRecordById(id);
    }

    /**
     * 查询审批记录信息列表
     *
     * @param approvalRecord 审批记录信息
     * @return 审批记录信息
     */
    @Override
    public List<ApprovalRecord> selectApprovalRecordList(ApprovalRecord approvalRecord) {
        return approvalRecordMapper.selectApprovalRecordList(approvalRecord);
    }

    /**
     * 新增审批记录信息
     *
     * @param approvalRecord 审批记录信息
     * @return 结果
     */
    @Override
    public int insertApprovalRecord(ApprovalRecord approvalRecord) {


        return approvalRecordMapper.insertApprovalRecord(approvalRecord);
    }

    /**
     * 修改审批记录信息
     *
     * @param approvalRecord 审批记录信息
     * @return 结果
     */
    @Override
    public int updateApprovalRecord(ApprovalRecord approvalRecord) {
        return approvalRecordMapper.updateApprovalRecord(approvalRecord);
    }

    /**
     * 删除审批记录信息对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteApprovalRecordByIds(String ids) {
        return approvalRecordMapper.deleteApprovalRecordByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除审批记录信息信息
     *
     * @param id 审批记录信息ID
     * @return 结果
     */
    @Override
    public int deleteApprovalRecordById(Long id) {
        return approvalRecordMapper.deleteApprovalRecordById(id);
    }

    /**
     * 审批信息
     *
     * @param approvalRecord
     * @return
     */
    @Override
    public AjaxResult approvalReport(ApprovalRecord approvalRecord) {
        AjaxResult result = null;
        boolean saveFiag = false;
        Date nowDate = new Date();
        if (approvalRecord.getRecordId() != null) {
            // 送审记录
            approvalRecord.setApprovalLevel(1);
            IdReport idReport = reportService.selectIdReportById(approvalRecord.getRecordId());
            // 查询到审核人记录
            if (idReport != null) {
                String userId = approvalRecord.getApprovalUser().toString();

                String[] auditorIds = idReport.getAuditorId().split(",");

                for (int i = 0, auditorEnd = auditorIds.length; i < auditorEnd; i++) {
                    if (auditorIds[i].equals(userId)) {
                        saveFiag = true;
                        break;
                    }
                }
                String temp = idReport.getAppRemark();
                if (StringUtils.isNotEmpty(temp)) {
                    temp += approvalRecord.getApprovalUser() + ",";
                } else {
                    temp = approvalRecord.getApprovalUser().toString();
                }
                idReport.setAppRemark(temp);
            }
            approvalRecord.setApprovalTime(nowDate);
            approvalRecord.setCreateTime(nowDate);
            // 允许保存审批信息
            if (saveFiag) {
                result = AjaxResult.success("审批成功!");
                // 获取设置的审批人数,
                String approvalUserIds = idReport.getAuditorId();
                if (StringUtils.isEmpty(approvalUserIds)) {
                    result = AjaxResult.warn("没有找到审批人信息!");
                } else {
                    // 获取到审批人数
                    int userIdsCount = approvalUserIds.split(",").length;
                    // 审批数量
                    int recordCount = approvalRecordMapper.getApprovalCount(approvalRecord.getRecordId(), null);

                    // 最后的一次的审批为通过
                    if (approvalRecord.getApprovalStatus().equals(5)) {
                        // 最后一个审批人
                        if (recordCount == (userIdsCount - 1)) {
                            // 查询审批结果，其他审批也为通过
                            int approvalCount = approvalRecordMapper.getApprovalCount(approvalRecord.getRecordId(), 5L);
                            // 全票通过
                            if (approvalCount == (userIdsCount - 1)) {
                                // 通过
                                idReport.setStatus("5");
                            } else {
                                // 未通过，因为其他人审批没通过
                                idReport.setStatus("1");
                            }
                        } else {
                            // 通过
                            //idReport.setStatus("5");
                        }
                    } else {
                        // 未通过
                        idReport.setStatus("1");
                    }
                    idReport.setUpdateTime(nowDate);
                    // 审批信息
                    approvalRecordMapper.insertApprovalRecord(approvalRecord);
                    reportService.updateIdReport(idReport);
                }

            } else {
                result = AjaxResult.warn("您无审批权限!");
            }
        } else {
            result = AjaxResult.warn("缺少审批id");
        }

        return result;
    }

    /**
     * 删除审批记录信息
     *
     * @param recordId
     */
    @Override
    public void deleteRecord(Long recordId) {
        approvalRecordMapper.deleteApprovalRecordByRecordId(recordId);
    }

    @Override
    public AjaxResult addApprovalStaff(ApprovalRecord approvalRecord) {
        AjaxResult result = null;
        if (approvalRecord != null) {
            approvalRecord.setApprovalLevel(2);
            approvalRecord.setApprovalStatus(2);
            approvalRecord.setApprovalTime(new Date());
            // 审批信息
            approvalRecordMapper.insertApprovalRecord(approvalRecord);
            result = AjaxResult.success("添加成功!");
        }
        return result;
    }
}
